Phenotypic diversity is considered beneficial to the evolution of contingent cooperation, in which cooperators channel their help preferentially towards others of similar phenotypes. However, it remains largely unclear how phenotypic variation arises in the first place and thus leads to the construction of phenotypic complexity. Here we propose a mathematical model to study the coevolutionary dynamics of phenotypic diversity and contingent cooperation. Unlike previous models, our model does not assume any prescribed level of phenotypic diversity, but rather lets it be an evolvable trait. Each individual expresses one phenotype at a time and only the phenotypes expressed are visible to others. Moreover, individuals can differ in their potential of phenotypic variation, which is characterized by the number of distinct phenotypes they can randomly switch to. Each individual incurs a cost proportional to the number of potentially expressible phenotypes so as to retain phenotypic variation and expression. Our results show that phenotypic diversity coevolves with contingent cooperation under a wide range of conditions and that there exists an optimal level of phenotypic diversity best promoting contingent cooperation. It pays for contingent cooperators to elevate their potential of phenotypic variation, thereby increasing their opportunities of establishing cooperation via novel phenotypes, as these new phenotypes serve as secret tags that are difficult for defector to discover and chase after. We also find that evolved high levels of phenotypic diversity can occasionally collapse due to the invasion of defector mutants, suggesting that cooperation and phenotypic diversity can mutually reinforce each other. Thus, our results provide new insights into better understanding the coevolution of cooperation and phenotypic diversity.