This is a loaded question. There are lots of stories of people who run a larger motor than on the sticker rating. Sometimes there is no issue, as some boats seem underrated for one reason or another. Other times, the listed rating is most appropriate.
Either way, if you are checked by coast guard or police while on the water, could that be an issue? What if something happens on the water, could liability or insurance be an issue?
This being said, my friend's boat is rated for 20hp, he has a 40hp 2 stroke and a 8hp 4 stroke kicker hanging on the back. The boat is absolutely safe, stable, doesnt ride low in the back and the transom is completely solid after years and years of use. His boat is 'overbuilt' for the rating.
Also 40hp or 50hp 2 strokes weight a lot less than a 4 stroke of equivalent power. You won't really know what works for you boat until you try.
If that 50 is a good runner, and you can buy it. Go ahead and do it, see how it does. Take it easy, and if it works well, great. If not, list it for sale or trade and move down.
In some years and motors, the 40hp and 50hp weight difference is minimal, same block, just the carb, timing and some other small things making the extra 10hp.