I am trying to understand the definition of diagrams as functors in category theory. Following nLab, a diagram $D$ of shape $\mathcal I$ in category $\mathcal C$ is a functor $D:\mathcal I\rightarrow \mathcal C$, where $\mathcal I$ is a small category. Let's consider the following cospan:
$$\begin{array}{} && X \\ && \big\downarrow{g} \\ Y & \underset{f}\longrightarrow & Z \end{array}$$
Let $D$ be a functor that maps $X\mapsto C,Y\mapsto C,Z\mapsto C',f\mapsto h,g\mapsto h$ and identities in the obvious way. This yields the following diagram in $\mathcal C$:
$$C\underset{h}\longrightarrow C' $$
Identities are preserved, and there are no composites. We have $$D(f:Y\rightarrow Z)=D(f):D(Y)\rightarrow D(Z)=h:C\rightarrow C'$$ and $$D(g:X\rightarrow Z)=D(g):D(X)\rightarrow D(Z)=h:C\rightarrow C'$$ as required.
Still, this functor does not preserve the shape of $\mathcal I$. It is no longer a cospan. Or is there some other sense in which the diagram $D$ has the shape of $\mathcal I$?
It does have the required shape:
$\require{AMScd}$ \begin{CD} @. C\\ @. @VV h V\\ C @>>h> C' \end{CD}
You can think of an indexed family $\{x_i\}_I$, which is a function $x\colon I\to X$. Let's say $I = 2$ and you have $x\colon 2\to X$. Then, $\{x_0,x_1\}_2$ is a family indexed by $2$ even when $x_0 = x_1$.
It's the same with the above diagram, it is indexed by $\mathcal I$ even though the arrows are the same morphism.
Actually, such diagrams are useful. One example is that you can say $h$ is monomorphism if and only if
\begin{CD} C @>\operatorname{id}_C>> C\\ @V \operatorname{id}_C V V @VV h V\\ C @>>h> C' \end{CD}
is Cartesian square (pullback).