Is using an underscore in user mentions as a replacement for a space a valid practice, and if not, then why? It feels obvious for me to do so, however the help on using
@username does not say anything about this.
No it is not. Doing so will result in the notification not working. One has to omit space, as does autocomplete. If one includes the space it will often still work, as the algorithm tries to match the start of the string. But including an underscore (same for other characters) will break notification.
To make the "often" explicit, if the part before the space is at least three characters long it will notify some user (and the intended one except if there is another user in the conversation that has a similar user name).
To answer the comment as regards the reason, this is difficult to answer with certitude (and I am not personally familiar with such things). Maybe for simplicity of the matching algorithm. Then, if I recall correctly, underscore is not a valid character in a username, thus indeed it could be done without too much trouble I suppose. Yet, then somebody might want to use a hyphen instead of an underscore, and so on. I think in the end it is just what seemed the right thing to do for the person that implemented it way back then. They might well have decided something else then.