Fight The Enemy, Not Your Weapons
The leading responses are all assuming an enemy similarly equipped, with similar tactics, when it is clear that asymmetric warfare is the most common sort of conflict. The only reason to use a mech with a melee weapon - or indeed a mech at all - is if that is the ideal weapon for the enemy you're facing.
Such an enemy would be one that is most easily countered by a larger-than-life metal humanoid with melee weapons. These enemies would include ones who are easily intimidated by size, who are numerous enough that ammunition is a problem, who are embedded enough that supply lines are a problem, or who are constructed in such a way that projectile weapons are insufficient or inefficient (how many .50 caliber rounds does it take to stop a triceratops?)
Note that while sticking artillery on a mecha seems 'a natural step', it's not necessarily the case that artillery is needed for the enemy at hand. If the mecha is primarily a sensor platform, for instance, which must be mobile over a variety of terrain but doesn't usually partake in combat. Or, the artillery in question is so long range (such as surface to space artillery) that using it in direct combat is impractical, then backup weapons are preferable which are, in turn, geared towards the sort of combat you're likely to face. Which brings us to...
Equip For The Job At Hand
About this image, ask yourself: why didn't they just use rockets? Contrary to a lot of answers here, urban warfare may be exactly why you want a mech with 'melee' weapons not because you protect the environment but because you are more dangerous to the environment. The ability to, on a rolling basis, demolish some or all of the environment you're in is a large tactical advantage. Such 'combat engineer mecha' can reduce enemy entrenchments and build your own while being protected against unexpected (or expected) attack. In this case the enemy is buildings, or open fields that need ditches, and the melee weapon (read: 'fancy shovel') is the best thing for the job.
While it is a narrow set of circumstances where a large, bipedal mechanized humanoid is the best weapon of choice, should you also need a tool for your mecha for jobs such as destroying buildings or entrenching open ground, we can assume that pilots will quickly learn to use that tool to squash endless waves of mimics - you know, should they need to.
Likewise, if the requirement is that the pilot is armored and unsupported for long periods over treacherous terrain, mecha may present a better option than, say, helicopters or other high-fuel devices or tanks with their reliance on some form of flatish ground. (Obviously there are different problems: the ability of the ground to support the mecha, etc. etc.)
Be Aware of Your Environment
Is the cargo loader from Aliens the ideal mechanized combat instrument? No. But is it an effective one? Yes. Why? First, it equalizes the disadvantages the human has: provides armor, size and strength against an opponent that is larger, stronger and has blades. Secondly, and most importantly, it doesn't risk turning the environment against the humans. In this case, the environment is a space ship that, if breached, will mean defeat. Rockets and projectiles in this situation are exactly the wrong sort of weapon - for the same reason the marines should have avoided using firearms when underneath the main cooling tower for the nuclear reactor.
In any situation where targets in the 'background' present a very high risk should they be inadvertently damaged, one must choose a weapon that reduces the chance of that happening. Space is one such environment: not only do bullets and rockets whizzing around often defeat the purpose of destroying your enemy, but due to Newton's Third Law (and indeed, the other two) shooting a bullet means you're forever accepting an equal and opposite trajectory (I'm eliding the actual physics here). Swinging an axe presents a different but more easily managed physics problem.
Be In Control of Your Equipment
While it has been mentioned that projectiles can be stopped or confused in several other answers, this concept has not been abstracted to the general case: while that bullet/rocket/rock currently arcing towards your target might be 'yours', it is only that way so long as no one else messes with it.
In a world comprised of EMPs, hacking, shields and other mishagus, once an object leaves your orbit of control you can't truly count on it being 'yours'. (And, notably, combat lasers and other 'beam' weapons might be prohibitively hard to make work, so 'instant hit' weaponry may not be on the table.) The nice thing about using a weapon physically attached your vehicle is that you are in direct control of it: if you lose that control you've already lost control of your vehicle, which is the far worse problem. The hardened electronics, active EW and black ice that is cost-effective for your vehicle but not for your rocketry can also protect a melee weapon. And, for that matter, in a super-high-tech environment, 'dumb' weapons generally need less protection (even if they are riskier).