Addition: when you choose a tonality in which you write a piece of music, it may define its standard set of flats and sharps, to simplify building chords using uniform rules. Because of this, it is convenient to name the same note using different names, relative to its neighbors.
Expansion: in non-tempered, natural tuning, such as often used when playing a violin, there are differences between some sharps and flats built from different notes, because natural harmonic intervals, based on frequency ratios like 3:2, do not split the frequency range in a completely log-linear way. This is why, say, G# and Ab may be not the same for purposes of pure natural harmony [1].
Equal temperation was invented to overcome this. J.S.Bach wrote a great showcase for it, Woll-Tepmeriertee Klavier, which involves harmonies and chord progressions that are hard or impossible to achieve with natural tuning without producing weird dissonances.
[1]: https://pages.mtu.edu/~suits/WhyG.html