The `dimname` variable is only applicable to when there are **no** coordinates associated with this dimension i.e. the `else` clause.

Therefore, move this statement to within the `else` clause.