The reasons for Mac can't boot from a USB drive could be a poor connection, incompatible macOS installer, incorrect Startup Security Utility settings, drive corruption, etc., you can fix these possible causes to boot a Mac from a USB drive successfully.
Apple enables you to boot Mac from USB to upgrade/downgrade macOS, try new macOS, and for other reasons. With the bootable USB drive connected, you can select the startup disk by holding the Option/Alt key(Intel Mac) or power button(Apple silicon Mac) while restarting your Mac.
If you only see the bootable HDD, no USB drive to choose from, and "the flash drive works without problems when the MacBook is loaded," then the connection should be in good condition, and the USB drive should not be corrupted. Otherwise, you may need to repair the drive in Disk Utility and recover data from it with iBoysoft Data Recovery for Mac.
Now, you can proceed to check the compatibility of the macOS installer, and the Startup Security Utility settings.
Is the macOS installed on the USB drive compatible with your Mac model? If not, you can't boot Mac from it. Check your Mac model from the Apple menu > About This Mac, and visit support.apple.com to see whether the macOS version you wanted supports your Mac model.
Then, check the Startup Security Settings on Intel Mac, perhaps, you don't allow booting from external media. You need to boot into macOS Recovery Mode to access Startup Security Settings from the menu bar, check Allow booting from external or removable media under Allowed Boot Media, then try to boot Mac from USB.