This is an interesting question because you've added that bit about compound nouns.
After all, there are some compound nouns that indeed use the word soldier, such as:
- mercenary soldier
- citizen soldier
However, coward soldier isn't a valid compound noun, and therefore you would want to use the form with the adjective: cowardly soldier.
The tricky part is explaining why. My first guess was:
We don't usually form compound nouns with modifiers that are easily converted into adjectives.
That might explain your first example compound, war hero. It's difficult to change war into an adjective. (Sure, there's the adjective warlike, but that has a different meaning and it would change the meaning of the compound lexeme.)
But then I thought about your second example: ghost story. If a "ghost story" is a story about a ghost (and "ghostly story" is not), then why couldn't a "coward soldier" be a soldier who behaved like a coward, rather than "cowardly soldier"? I don't really have a good answer to that, and I'm not sure there is one. As one blogger wrote:
As Ronald Carter and Michael McCarthy put it in "Cambridge Grammar of English," the compound noun structure is "extremely varied in the types of meaning relations it can indicate," from what the object is for like waste-paper basket to what something is made of like woodpile or metal slab, how something works like a convection oven to what someone does like a language teacher.
As a result, usage rules for everything from punctuation to capitalization can be confusing, especially for new English grammar learners.
Sometimes there's simply not an easy rule to explain the way things are the way that they are, and work the way that they do.