In real life, if there was a masked vigilante whose codename is ‘Watcher,’ he’s a non-lethal masked vigilante who is a witness to a murder. The detectives and prosecutor want to put the vigilante on the witness stand, but the vigilante’s secret identity is, well, secret, and he doesn’t want to reveal it. Can he still testify with his mask on or testify anonymously?


Putting somebody on the stand doesn’t mean they’re on trial. Witnesses tend to be as public as the rest of the trial. So this could be not public at all due to factors like children having to give testimony or state security being on the line. I’m sure there are more reasons and those may differ from one jurisdiction to the next. Or the trial is public.
There may be rules to safeguard the identity of witnesses, such as principal witnesses who just by speaking out endanger their lives (e.g. in organized crime). A vigilante is by definition doing illegal stuff so they may refuse to give evidence based on the fact they would have to incriminate themselves. But I don’t think any court would entertain the idea of having a Batman figure in costume take the stand to give evidence. By protecting the identity the court would tacitly approve of illegal vigilantism. That probably only works in comic books.