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).
65Walking mechs are perhaps the most impractical weapons platform of anything having equal prominence in fiction. They are useful only when the terrain is too rough for tanks, enemy air control is too effective for friendly air ops, and the environment is lethal to unprotected infantry. Otherwise you are better off using tanks, helicopters, or infantry (the latter possibly in powered assault armor). – EvilSnack – 2016-08-21T17:15:09.097
26Why are people down-voting this? Sky does acknowledge how silly mechs are, and the fact that they are silly to use in this manner is immaterial to the actual question, which is reasonable. – Marky – 2016-08-21T17:23:03.647
8@Marky Assuming it is silly to use big robots, describe why it is not silly to have big robots knife fights in a gun battle. Sounds like a silly question in a silly world and another "justify the rule of cool" question. – Aron – 2016-08-22T02:56:52.080
11@Aron are "justify the rule of cool" questions not allowed on worldbuilding then? – Marky – 2016-08-22T03:18:43.023
5@Marky By the very definition of "Rule of Cool" it is not justifiable. If it was justifiable then it wouldn't be "Rule of Cool"; it would be a really cool thing that happened to be a thing, like Ninjas. – Aron – 2016-08-22T03:25:10.473
Did you read Muv-Luv (Alternative) ? They have pretty believable reason for melee weapons for their mechs (it basically boils down to what EvilSnack said). Well, only against aliens. When it is mech vs. mech, melee weapons are rarelly used and the guys with better ranged weapons usually win. – Euphoric – 2016-08-22T08:12:35.787
2To avoid civilian casualties? If you fight in a city, and your miss with those rockets, you will do some real damage to the population. Depending on the political situation it might no be worth the risk – MTilsted – 2016-08-22T11:19:08.650
2The excuse you see most often for swords is that they are a backup weapon, something that does not jam and does not run out of bullets. In real life, edged weapons are completely useless. Nobody carries swords anymore and bayonets are being phased out. – ventsyv – 2016-08-22T13:12:44.210
3@ventsyv As an issued thing, yes. Soldiers will still carry personal knives and blades on their own for utility and "just in case" – Marky – 2016-08-22T13:40:09.397
2Actually the green berets in America use combat shovels. Which are more than capable of chopping a head in half or digging a grave, whichever to your liking. – Skye – 2016-08-22T13:41:54.687
1
Nevermind the Americans, the Chinese brought it to the next level. https://youtu.be/VzpRh-ZE9Mo
– Skye – 2016-08-22T13:53:02.320One thing about mecha compared to tanks, etc, is that they more closely resemble the human body structure. Maybe any average joe can jump into the VR controller and statt walking the mech around intuitively. Just don't push the big red buttons. – DoubleDouble – 2016-08-22T14:13:39.267
3I think it was one (or many) of the Gundam series in which the combatants always dispersed some passive jamming technology at the beginning of battles. IIRC It was basically a large cloud of tiny prisms and reflective particles, that played havoc with radio and other rf based technologies (including beam weapons) by dispersion and refraction of the signals. Larger caliber rounds were still viable, but largely ineffective against the Gundam's armor, Nothing using guidance technology worked... Thus falling back to melee combat between robots. – Mr.Mindor – 2016-08-22T15:43:42.713
2@EvilSnack isn't a mech simply an infantry soldier in a (albeit rather large) powered assault armour? – gbjbaanb – 2016-08-22T20:30:01.550
Because sometimes, your inner 12-year-old wants to see giant robots fighting giant aliens using a train as a club
– None – 2016-08-24T13:40:36.100Mostly for show. Regardless of how fast "huge ass robots" are presented in mindless movies, physics dictates those things just can't get nimble enough to use melee weapons effectively. A shield would make more sense than a melee weapon per se. Or some tool that servers a specific purpose, like digging or drilling. – dtech – 2016-08-25T11:30:04.603
Who is bringing zombies errr mechs back to life? – James – 2016-08-25T13:57:38.527
@EvilSnack that actually sounds like a plausible scenario on a planet with very rough terrain and air that would prohibit flying, such as being very thin or heavy with particulate matter that would damage conventional jet engines. – shadowtalker – 2016-08-25T17:20:42.760
@ddriver that's a more plausible objection to me. I imagine they'd look more like what you find on Battlebots: hooks and spikes and saws on triggers, as opposed to something like in Gundam (i.e. a hand attached to an arm gripping a sword). This would also resemble how "melee weapons" were used in ancient naval combat: to ram, disable, and board the enemy's boat. – shadowtalker – 2016-08-25T17:24:13.663
Because when you challenge a guy with a big robot, even if you added really big guns, it may turn out to just be the rules
– J... – 2016-08-25T20:19:00.417