If galaxies are moving away from each other then why are the Milky Way and Andromeda galaxy coming towards each other?
The Andromeda Galaxy is approaching the Milky Way at about 684000 mi/hours, making it one of the few blueshifted galaxies. The Andromeda Galaxy and the Milky Way are thus expected to collide in about 3.75 or 4.5 billion years.
Why are some galaxies moving away and why are our galaxy and Andromeda coming towards each other?
Here is my answer to a similar question posted on the physics stack exchange website.
Hubble's law (the law that deals with the expansion of the universe) applies to the expansion of space itself, i.e., if two objects stationary to each other that had no force between them were left alone the distance between would increase with time because space itself is expanding. This is what Hubble's law addresses.
In the case of the Milky Way and Andromeda galaxies (and all galaxies for that matter) there is a force between them: gravity. The gravitational force between the Milky Way and Andromeda galaxies has produced an acceleration that is causing the two galaxies to be moving towards each other faster than the space between them is expanding as calculated by Hubble's law. However, the vast majority of galaxies lie far enough away from the Milky Way that the gravitational force between us and them is small compared to the Hubble expansion and Hubble's law dominates.
In short, Hubble's law applies throughout the universe, but localized systems may have enough gravitational attraction between them that the gravitational effects dominate