The expansion of space on large scales does not directly affect the motion of galaxies within a local group, such as the Milky Way and the Andromeda galaxy. While it is true that the space between galaxies is expanding at cosmological scales, the gravitational attraction between galaxies overcomes the expansion on smaller scales.
In the case of the Milky Way and the Andromeda galaxy, they are part of the Local Group, a collection of several dozen galaxies that are gravitationally bound to each other. The gravitational forces between these galaxies are stronger than the effect of the expanding space.
The expansion of the universe primarily affects the space between galaxy clusters or superclusters, where the distances are much larger. At these larger scales, the expansion of space becomes significant, causing the separation between clusters to increase over time.
However, within a local group like the Milky Way and Andromeda, the gravitational attraction dominates. Both galaxies are moving toward each other under the influence of gravity and are expected to collide in the future. This collision is not a consequence of the expanding universe but rather the result of their mutual gravitational interaction within the local group.
So, while the expansion of space affects the large-scale structure of the universe, it does not hinder the gravitational interactions between galaxies within smaller, gravitationally bound systems like the Local Group.