Most astronomers think that the red shift we see from other galaxies is evidence that the universe is expanding. Not all astronomers think this (Halton Arp is one who disagrees), but most do. However, the light coming from the Andromeda galaxy has a BLUE shift, which indicates motion towards us.
Although no astronomer can say for sure, most think that Andromeda might have had a collision (or a near miss) with another galaxy (Maffei 1) in the past. This collision could have caused the Andromeda galaxy to move in the opposite direction. This is similar to watching a toy boat floating down a stream. In general, it moves with the stream. However, a collision with a rock might send it upstream for a bit until the current of the stream overtakes it again.