Currently Italy has a mask mandate inside long distance trains. I am to take a train from Italy to Switzerland, where masks are not mandatory anywhere.
Once the train crosses the border into Switzerland, can I legally remove my mask?
Just because there is not a government mandate to mask doesn't necessarily mean that the train operator doesn't have their own rules. For example, Trenitalia currently says (Google Translate): "Its necessary to wear an FFP2 type mask to protect the nose and mouth for the duration of the trip". The best way to find out for sure would be to ask the conductor about it - they'll know the latest rules and regulations aboard their own train.
That being said, Italy is expected to lift their masking rules by May, so this question should cease to be relevant as masking will become voluntary in both nations.