You don't say why you want to do this. But if your hope is to obfuscate your identity to reduce tracking, you can use Adguard for Mac https://adguard.com/en/adguard-mac/overview.html which allows modifying the User-Agent in its advanced settings.
You have, I take it, explored what you can do under Safari's Develop menu.