Why is category.getUrl() returning incorrect subdomain?
Looks like the problem was using @web for the 'Base URL' setting in 'Settings > Sites'.

I replaced this with an environment variable BASE_URL="https://www.domain.com" defined in the .env file, and setting $BASE_URL in the 'Base URL' field. 

No idea why @web doesn't work reliably here, but so be it. 

Feel free to delete this question, but I thought I would leave it for others who may encounter the same problem.