The idea that the Andromeda Galaxy will collide with the Milky Way in the future is actually unrelated to the expansion of the universe. While it is true that the universe is expanding on large scales, the force responsible for the eventual collision between Andromeda and the Milky Way is gravity.
Both the Andromeda Galaxy and the Milky Way Galaxy are part of a larger galactic group known as the Local Group, which consists of dozens of galaxies. Within the Local Group, the gravitational interactions between galaxies can overcome the effects of the universe's expansion.
Currently, the Andromeda Galaxy and the Milky Way are moving towards each other due to their gravitational attraction. The expansion of the universe does not have a significant effect on the motion of galaxies within relatively close distances like those within the Local Group.
Over the course of billions of years, the gravitational pull between Andromeda and the Milky Way will cause them to approach each other. Eventually, their gravitational interaction will become so strong that they will merge, forming a new, larger galaxy. This collision is estimated to occur around 4.5 billion years from now.
So, while the universe's expansion is causing galaxies to move away from each other on large scales, local gravitational interactions can overcome this expansion and lead to collisions between galaxies like the future collision of Andromeda and the Milky Way.