On a hotel or café network, other devices can probe for computers that answer. The macOS firewall has an option that makes your Mac keep quiet.
In System Settings, click Network, then Firewall. The firewall has to be on. Click Options and turn on Enable stealth mode.
System Settings > Network > Firewall > Options
Enable stealth mode: on
Apple describes it like this: your Mac stops responding to probing requests that can reveal its existence. Authorized apps still get answers, but unauthorized requests such as ICMP ping get none. To someone scanning the network, the Mac looks less like a target.
You can read the current state in Terminal without changing anything:
/usr/libexec/ApplicationFirewall/socketfilterfw --getstealthmode
I’d turn it on for a laptop that travels. On a desktop that never leaves your own network it matters less.
The flip side is troubleshooting. With stealth mode on, ping from another computer to your Mac gets no reply, even at home. That’s expected, so don’t spend an evening hunting a network fault: test with the service you actually want to reach instead, or switch it off briefly while you test. It’s also not a replacement for keeping sharing services off that you don’t use, which Apple points out can still let connections through.
Further reading: Change Firewall settings on Mac (Apple), socketfilterfw.8 man page