A problem, indeed. That is why subs back then, and today, make an effort to determine the type and mission of the ship. . .except for Italy and Germany in WWII. I don't recall ALL belligerents engaged in unrestricted sub warfare. I believe we didn't do that.
Ref bomber pilots or artillery. . .spotters, forward air controllers and targeting Intel play that role.
Ah, but we did. We very specifically authorized the USN to engage in "unrestricted submarine warfare". As such, every Maru in the Pacific was fair game. And that is why public dispatches in WW II did not identify submarine capatains and crewmen by name (at least, I believe that's the case).
One target sunk by a US submarine, for example, was a "prison ship" which was transporting POWs from The Phillipines to Japan. There are several such cases, reported here.
There is simply no practical way for a submarine commander to determine whether a merchant freighter is carrying military personnel and/or materiel. But, in a war zone, an enemy-flagged vessel is presumed to do so.