Vanity and pride are different things, though the words are often used synonymously. A person may be proud without being vain. Pride relates more to our opinion of ourselves; vanity, to what we would have others think of us.
– Jane Austen
Why peacock displays its feathers to attract peahen was puzzle for Darwin. This display of feathers not only attracted peahen but also attracted attention of predators like tigers and leopards. By displaying his feathers peacock was risking his life.
Israeli biologists, Amotz and Avishag Zahavi came with an explanation called handicap principle. To support their hypothesis they applied game theory to biology esp. Michael Spence’s job-market signalling model, where a potential employee sends a signal to employer about his ability to do job by acquiring degree from a good college.
Amotz applied this concept to behaviour of peacock. By displaying feathers the peacock sends a signal to peahens that he can afford this handicap of coloured feathers (inspite of risk) and therefore is ideal mate for peahen. Also those lacking such colourful display should not compete with him. This is similar to job market signalling model, where employer is ready to pay more to good employee (i.e. those with degree from premier college) than bad employees (those from average colleges).
Another example in animal world is that of stotting of gazelles, when attacked by predator the gazelle starts stotting i.e. jump higher instead of running faster. By stotting the gazelle sends a signal to leopard that has lot of energy and is in good health, so leopard should not waste his time chasing him, as leopard is unlikely to hunt him. It has been found that leopards avoid going after stotting gazelles.
Handicap principle is also applicable to humans; ownership of goods of premier brands and showing off sends a signal to others that owner is capable of bearing expenses of goods and therefore is a desirable partner. Thus vanity may not be morally desirable, but it definitely has useful purpose.