The UN is a diplomatic organization that represents the entire world and exists to provide non-violent outlets for issues between nations. Being present at BRICS, which is rapidly adding membership and represents a counter-bloc to Western oriented alliances like NATO and the G7 (which expelled Russia in 2014.) is part of that mission.
Guterres was at the G7 summit https://www.un.org/sg/en/content/sg/press-encounter/2023-05-21/secretary-generals-press-encounter-g7-summit
Why shouldn't he be at a similar summit representing:
Combined, the BRICS members encompass about 30% of the world's land surface and 45% of world population
The UN isn't (rather: shouldn't be) a Western organization. It's a global organisation. His attendance there is a necessary part of his duties.
From the perspective of any nation on the receiving end of US aggression his participation at the G7 would have been seen as equally upsetting. You're letting your bias in favor of Ukraine color your judgement.
If we want to uphold the principle of international diplomacy and the summit represents a huge bloc of the world's population then yes he really should. If Israel is hosting an event for dozens of countries he should. He's not just visiting his buddy Putin or Netenyahu. He's the main diplomat of the UN.